<script setup>
import { ElMessage } from 'element-plus';
import Layer from "@/components/layer/index.vue";
import { defineProps, ref, defineEmits } from 'vue'
import { addAPI, updateAPI } from '@/api/wedding/couponInfo'

// 使用defineEmits来接收来自父组件的方法
const emit = defineEmits(['getTableData'])
// 声明props
const props = defineProps({
  layer: {
    type: Object,
    default: () => {
      return {
        show: false,
        title: '',
        showButton: true
      }
    }
  }
});

/************************************定义变量************************************************/
const ruleForm = ref(null)
const layerDom = ref(null)
const rules = {
  amount: [{ required: true, message: '请输入优惠卷金额', trigger: 'blur' }],
  validityDay: [{ required: true, message: '请输入有效期', trigger: 'blur' }],
}
let form = ref({
  id: null,
  userId: null,
  orderId: null,
  orderCode: null,
  amount: null,
  validityDay: null,
  limitTime: null,
  status: null,
  type: null,
  isUse: null,
  createTime: null,
  updateTime: null,
  delFlag: null,
})

/************************************数据回显************************************************/
if (props.layer.row) {
  if (props.layer.row) {
    let source = props.layer.row
    Object.keys(form.value).forEach(k => k in source && (form.value[k] = source[k]));
  }
}

/************************************定义事件************************************************/
//提交按钮事件
let submit = () => {
  if (ruleForm) {
    ruleForm.value.validate((valid) => {
      if (valid) {
        let params = form.value
        params.type = '0'
        console.log('[ params ] >', params)
        if (props.layer.row) {
          updateForm(params)
        } else {
          addForm(params)
        }
      } else {
        return false;
      }
    });
  }
}

let updateForm = (params) => {
  updateAPI(params)
    .then(res => {
      ElMessage({
        type: 'success',
        message: '编辑成功'
      })
      emit('getTableData', false)
      layerDom && layerDom.value.close()
    })
}

let addForm = (params) => {
  addAPI(params)
    .then(res => {
      ElMessage({
        type: 'success',
        message: '新增成功'
      })
      emit('getTableData', true)
      layerDom && layerDom.value.close()
    })
}
</script>

<template>
  <Layer :layer="layer" @confirm="submit" ref="layerDom">
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px" style="margin-right:30px;">
      <el-form-item label="优惠卷金额：" prop="amount">
        <el-input-number size="large" v-model="form.amount" :precision="2" :step="0.1">
          <template #suffix><span>元</span></template>
        </el-input-number>
      </el-form-item>
      <el-form-item label="有效期：" prop="validityDay">
        <el-input-number size="large" v-model="form.validityDay" :precision="0" :step="1">
          <template #suffix><span>天</span></template>
        </el-input-number>
      </el-form-item>
    </el-form>
  </Layer>
</template>

<style scoped lang="scss">
.el-input-number--large {
  line-height: 38px;
  width: 230px;
}
</style>